package com.hectopascal.controller;

import com.hectopascal.BaseResult;
import com.hectopascal.auth.component.JwtTokenUtil;
import com.hectopascal.exception.ServiceException;
import com.hectopascal.service.UserService;
import com.hectopascal.utils.CurrentUser;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {

    @Autowired
    private UserService userService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/register")
    public String register(@RequestParam String username,
                           @RequestParam String password) {
        userService.register(username, password, "admin");
        return "注册成功";
    }

    @GetMapping("/home")
    public String home() {
        String username = CurrentUser.getUsername();
        throw new ServiceException("欢迎来到首页");
//        return "欢迎来到首页";
    }

    @GetMapping("/admin/dashboard")
    public BaseResult<String> admin() {
        String token = jwtTokenUtil.generateToken("hectopascal");
        return BaseResult.buildResult("欢迎来到管理员界面！");
    }
}